package com.paradise.phone.utils;
/*
 * 自定义拨号盘
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paradise.phone.R;

public class Numpad extends LinearLayout implements OnClickListener, View.OnTouchListener {

    private int styleLayout = R.layout.numpad;
    private int[] ids = {R.id.tv_zero, R.id.tv_one, R.id.tv_two,
            R.id.tv_three,R.id.tv_four, R.id.tv_five,
            R.id.tv_six, R.id.tv_seven, R.id.tv_eight,
            R.id.tv_nine, R.id.tv_star, R.id.tv_hash};

    private View rootView;

    private View[] digits = new View[12];

    private OnDigitClickListener listener;

    public Numpad(Context context) {
        super(context);
    }

    public Numpad(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray t = getContext().obtainStyledAttributes(attrs,R.styleable.Numpad);

        styleLayout = t.getResourceId(R.styleable.Numpad_style_layout, R.layout.numpad);

        initView(context);
    }

    private void initView(Context context){
        rootView = LayoutInflater.from(context).inflate(styleLayout, this, true);
        for(int i = 0; i < 12; i++){
            digits[i] = rootView.findViewById(ids[i]);
            digits[i].setOnClickListener(this);
            digits[i].setOnTouchListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        if (listener != null)
            for(int i = 0; i < 12; i++){
                if (v.getId() == ids[i]) {
                    listener.onDigitClickListener(v, ((TextView) v).getText().toString());
                }
            }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (listener != null)
            for(int i = 0; i < 12; i++){
                if (v.getId() == ids[i]) {
                    listener.onDigitTouchListener(v, ((TextView) v).getText().toString(), event);
                }
            }
        return false;
    }

    public void setOnDigitClickListener(OnDigitClickListener listener){
        this.listener = listener;
    }

    public interface OnDigitClickListener{
        void onDigitClickListener(View digit, String num);
        void onDigitTouchListener(View digit, String num, MotionEvent event);
    }


}
